package com.studyv3.test.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

public class FlagConverter implements Converter<Integer> {

    private static final String pass = "已通过";
    private static final String unapprove = "未审批";
    private static final String unpass = "未通过";
    private static final String recall = "撤回";


    @Override
    public Class<?> supportJavaTypeKey() {
        // 实体类中对象属性类型
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        // Excel中对应的CellData属性类型
        return CellDataTypeEnum.STRING;
    }
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 从Cell中读取数据
        String gender = cellData.getStringValue();
        // 判断Excel中的值，将其转换为预期的数值
        if (pass.equals(gender)) {
            return 0;
        } else if (unapprove.equals(gender)) {
            return 1;
        }else if (unpass.equals(gender)) {
            return 2;
        }else if (recall.equals(gender)) {
            return 3;
        }
        return null;
    }
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) throws Exception {
        if (context.getValue() == null) {
            return new WriteCellData<>("");
        } else if (context.getValue() == 0) {
            return new WriteCellData<>(pass);
        } else if (context.getValue() == 1) {
            return new WriteCellData<>(unapprove);
        }else if (context.getValue() == 2) {
            return new WriteCellData<>(unpass);
        }else if (context.getValue() == 3) {
            return new WriteCellData<>(recall);
        }
        return new WriteCellData<>("");
    }
}